package com.foodchain.controller;

import com.foodchain.consts.Code;
import com.foodchain.entity.Activity;
import com.foodchain.entity.ActivityContent;
import com.foodchain.service.ActivityService;
import com.foodchain.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
 * @ClassName 活动
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 11:01
 * @Version 1.0
 **/
@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController {

    @Resource
    private ActivityService activityService;
    /*
     * 功能描述:查询所有活动信息
     * @param:  * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:42
     */
    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "6") Integer size){
        checkStatus(); // 更新活动状态

        Page<Activity> data = activityService.findListAll(page,size);
        return JsonCode(0,"success",data);
    }
    /*
     * 功能描述:查询用户报名活动接口
     * @param:  * @param userId
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:41
     */
    @RequestMapping("/userList")
    @ResponseBody
    public String userList(
            @RequestParam(value = "userId") String userId){
        if (Misc.isEmpty(userId)) return toJson(false, "userId信息为空", null);
        List<Activity> data = activityService.findUserListAll(userId);
        return JsonCode(0,"success", data);
    }
    /*
     * 功能描述:根据活动id获取活动内容
     * @param:  * @param activityId
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/27 17:24
     */
    @RequestMapping("/getcontent")
    @ResponseBody
    public String getContent(@RequestParam(value = "activityId") String activityId){
        Activity content = activityService.getContent(activityId);
        return JsonCode(0,"success",content);
    }

    private void checkStatus() {
        // 未开始的活动，状态更改为1
        List<Activity> actSign = activityService.findActSign();
        for (Activity act : actSign) {
            if (Misc.isEmpty(act.getStatus()) || act.getStatus() != 1) {
                act.setStatus(Integer.valueOf(Code.ActivityStatus.SIGNING));
                activityService.updateStatus(act);
            }
        }

        // 进行中的活动，状态不为2的更改为2
        List<Activity> actIng = activityService.findActIng();
        for (Activity act : actIng) {
            if (Misc.isEmpty(act.getStatus()) || act.getStatus() != 2) {
                act.setStatus(Integer.valueOf(Code.ActivityStatus.HAVING));
                activityService.updateStatus(act);
            }
        }

        // 已结束的活动，状态更改为3
        List<Activity> actEnd = activityService.findActEnd();
        for (Activity act : actEnd) {
            if (Misc.isEmpty(act.getStatus()) || act.getStatus() != 3) {
                act.setStatus(Integer.valueOf(Code.ActivityStatus.END));
                activityService.updateStatus(act);
            }
        }
    }
}
